#include "TurnToBehaviour.h"

/*
 * Spravanie otoci telo na urcity bod zadany ako parameter.
 *
 * @param	position	bod, na ktory sa ma hrac otocit
 */
void TurnToBehaviour::Behave(const Point& position)
{
	DATA_STORAGE.turnNeck.futurePosition = position;
	DATA_STORAGE.turnNeck.futureMyPosition = world->me->pos;
	DATA_STORAGE.turnNeck.nextTurn = 0;

	Angle dir = CalcDir(position - world->me->pos, world->me->GetAbsDir());
	if(dir.GetAbsDeg() >= 1)
		commandGenerator->Turn(dir.GetDeg());
}

/*
 * Kompenzovanie momentu otacania.
 *
 * @param turnMoment	hodnota momentu otacania, ktory mame kompenzovat
 *
 * @return	cislo urcujuce velkost potrebnej kompenzacie
 */
float TurnToBehaviour::CompTurn(float turnMoment)
{
	return turnMoment / (1 + world->me->vel.GetLength() * world->me->type->inertia_moment);
}

/*
 * Funkcia na predikovanie stavu hraca po prikaze turn
 *
 * @param me odkaz na mna (hraca)
 * @param moment moment otocenia, po ktorom sa ma predikovat
 */
Myself TurnToBehaviour::AfterTurn(const Myself& me, float moment)
{
	//zmeni sa pozicia, vektor rychlosti a uhol otocenia
	Myself tmpMe(me);
	tmpMe.Step();
	DegAngle tmpAngle(CompTurn(moment));
	tmpMe.GetAbsDir() += tmpAngle;
	return tmpMe;
}

/*
 * prikaz turn pre brankara, navyse sa pozrie za loptou
 *
 * @param 
 */
void TurnToBehaviour::GoalieTurn(float turnMoment)
{
	Ball tmpBall(*world->ball); tmpBall.Step();
	commandGenerator->Turn(turnMoment);
	GetTurnNeckToBehaviour()->Behave(tmpBall.pos, world->me->pos + world->me->vel, CompTurn(turnMoment));
}
